package com.itheima.controller;

import com.itheima.aop.Log;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.service.impl.EmpServiceImpl;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.coyote.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    EmpService empService;
    //条件分页查询
    @GetMapping
    public Result pageWhere(@RequestParam(defaultValue = "1")Integer page,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            String name, Integer gender,
                            @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate begin,
                            @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end){
        log.info("分页条件查询员工数据:{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        PageBean pageBean = empService.empInfo2(page,pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }


    //批量删除员工
    @Log
    @DeleteMapping("/{ids}")
    public Result deleteEmp(@PathVariable List<Integer> ids){
        log.info("批量删除员工:{}",ids);
         empService.deleteEmp(ids);
         return Result.success();
    }

    //添加员工
    @Log
    @PostMapping
    public Result addEmp(@RequestBody Emp emp){
        log.info("新增员工,emp:{}",emp);
        empService.addEmp(emp);
        return Result.success();

    }

    //根据id查询员工
    @GetMapping("/{id}")
    public Result selectEmpById(@PathVariable Integer id){
        log.info("根据id查询员工:id:{}",id);
        Emp emp = empService.selectEmpById(id);
        return Result.success(emp);
    }

    //修改员工信息
    @Log
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp){
        log.info("修改员工信息:emp:{}",emp);
        empService.updateEmp(emp);
        return Result.success(emp);
    }
}
